﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NHibernate;
using DotNet.Common.NHibernateUtil;
using ZLCMS.Data.Model;
using NHibernate.Criterion;
using ZLCMS.Business.Util;

namespace ZLCMS.Web.Interface.weixin.Html
{
    public partial class WxApplyMemberCardHtml : UrlRewritePage
    {
        public string owner;
        public string openId;
        public WxMemberCard _memberCard;
        public WxMemberCardConfig _Config;
        protected void Page_Load(object sender, EventArgs e)
        {
            owner = Request["account"];
            openId = Request["openid"];
        }

        #region 申请会员卡 + protected void btnApply_Click(object sender, EventArgs e)
        /// <summary>
        /// 申请会员卡
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnApply_Click(object sender, EventArgs e)
        {
            using (ISession session = SessionFactoryHelper.CreateSession())
            {
                //新增会员卡记录
                try
                {
                    _memberCard = session.CreateCriteria<WxMemberCard>().Add(Expression.Eq("WxId", openId)).Add(Expression.Eq("OwnerAccount", owner)).UniqueResult<WxMemberCard>();

                    if (_memberCard == null)
                    {
                        _memberCard = new WxMemberCard();

                        _Config = session.CreateQuery("from WxMemberCardConfig where OwnerAccount=:owner")
                        .SetParameter("owner", owner)
                        .UniqueResult<WxMemberCardConfig>();

                        _memberCard.CardConfigId = _Config.Id;
                        _memberCard.MemberAccount = owner;
                        _memberCard.Status = "1";
                        _memberCard.ApplyDate = DateTime.Now;
                        _memberCard.OwnerAccount = owner;
                        session.Save(_memberCard);
                        _memberCard.CardNo = _Config.CardHeadChar + _memberCard.Id.ToString("00000000");
                        session.Save(_memberCard);
                        session.Flush();

                        Response.Write("申请成功!您的会员卡号是:" + _memberCard.CardNo);
                        //Response.End();
                        Response.Redirect("/Interface/weixin/Html/WxMemberCardHtml.aspx?account=" + owner + "&openid=" + openId);
                    }
                    else
                    {
                        Response.Write("您已经申请过会员卡了.");
                    }

                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);;
                }
            }
        }
        #endregion

        #region 绑定会员卡 + protected void btnBind_Click(object sender, EventArgs e)
        /// <summary>
        /// 绑定会员卡
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnBind_Click(object sender, EventArgs e)
        {
            Response.Redirect("/Interface/weixin/Html/WxBindMemberHtml.aspx?account=" + owner + "&openid=" + openId);
        }
        #endregion
    }
}